var express = require('express');
var http = require('http');

var app = express();

var handlers = require('./requestHandlers');
var m_db_session = null;

var httpServer = null;

var socketio = require('socket.io');
var socketServer = null;

var RefMgr = require('./referenceMgr').GetInstance();

function start(db_session){
    
    console.log("Initialize Server...");
    
    m_db_session = db_session;
    
    app.use(express.cookieParser());
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(app.router);
    
    //initialize references..
    console.log("starting load references..");
    RefMgr.Initialize();
    
    //initialize handlers..
    handlers.Initialize(app, m_db_session);
    
    httpServer = http.createServer(app);
    httpServer.listen(80);
    
    socketServer = socketio.listen(httpServer);
    socketServer.sockets.on('connection', function (socket) {
                            socketServer.sockets.emit('connection', 'hello world');
                            console.log("a new conncetion!!");
                            });
}

exports.start_server = start;
